package com.zhilehuo.controller;


import com.github.pagehelper.PageInfo;
import com.zhilehuo.common.BaseResponse;
import com.zhilehuo.model.DTO.ParagraphWithWords;
import com.zhilehuo.model.po.Article;
import com.zhilehuo.model.request.ArticleRequest;
import com.zhilehuo.model.vo.ArticleVo;
import com.zhilehuo.service.IArticleService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/article")
@CrossOrigin(origins = "*")
public class ArticleController {

    @Resource
    IArticleService articleService;

    //分页查询、 可以根据 类型或者 文章标题来查询
    @RequestMapping(value = "/page",method = RequestMethod.POST)
    public BaseResponse<PageInfo<ArticleVo>> query(@RequestBody ArticleRequest articleRequest) {
        // 从请求中获取查询参数
        String title = articleRequest.getTitle();
        String category = articleRequest.getCategory();
        Integer pageNum = articleRequest.getPageNum();
        Integer pageSize = articleRequest.getPageSize();

        if (category == null || category.isEmpty()) {
            category = null;
        }
        if (title == null || title.isEmpty()) {
            title = null;
        }
        // 调用 Service 层进行查询
        PageInfo<ArticleVo> pageInfo = articleService.queryArticles(title, category, pageNum, pageSize);

        return new BaseResponse<>(200, pageInfo);

    }


    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseResponse<Boolean> insert(@RequestBody Article article) {


        return articleService.insert(article);
    }


    @GetMapping("/{id}/content")
    public BaseResponse<List<ParagraphWithWords>> getArticleContentWithPinyin(@PathVariable Integer id) {
        return articleService.getArticleWithPinyin(id);
    }






}
